package com.example.service.impl;

import com.example.dao.ProductInventoryDao;
import com.example.pojo.dto.PageDTO;
import com.example.pojo.dto.ProductInventoryDTO;
import com.example.pojo.dto.SaveProductAllotApplyDTO;
import com.example.pojo.dto.UpdateProductAllotApplyDTO;
import com.example.pojo.orm.ProductInventory;
import com.example.pojo.vo.PageVO;
import com.example.service.ifac.ProductInventoryService;
import com.example.util.JsonResult;
import com.example.util.ResultCodeEM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 季鑫利
 * @description 商品库存接口实现类
 */
@Service
public class ProductInventoryServiceImpl implements ProductInventoryService {

    /**
     * 商品库存dao
     */
    @Autowired
    private ProductInventoryDao productInventoryDao;

    /**
     * 新增/更新商品库存信息
     *
     * @param saveProductAllotApplyDTO 新增商品库存信息
     * @return true:成功 false：失败
     * @author 季鑫利
     */
    @Override
    public JsonResult saveProductInventory(@RequestBody SaveProductAllotApplyDTO saveProductAllotApplyDTO) {
        JsonResult result = new JsonResult();
        //判断参数合理性
        if (saveProductAllotApplyDTO.getModelNumber().isEmpty()
                || saveProductAllotApplyDTO.getUnit() == null
                || saveProductAllotApplyDTO.getUnit() != 1
                || saveProductAllotApplyDTO.getUnit() != 2
                || saveProductAllotApplyDTO.getProductId() == null
                || saveProductAllotApplyDTO.getProductId() <= 0
                || saveProductAllotApplyDTO.getTypeId() == null
                || saveProductAllotApplyDTO.getTypeId() <= 0
                || saveProductAllotApplyDTO.getTypeName().isEmpty()
                || saveProductAllotApplyDTO.getAllotNumber() == null
                || saveProductAllotApplyDTO.getAllotNumber() <= 0
                || saveProductAllotApplyDTO.getApplicant().isEmpty()
                || saveProductAllotApplyDTO.getCounterNumber().isEmpty()) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("参数不可为空");
            return result;
        }
        boolean bRet;
        bRet = productInventoryDao.saveProductInventory(saveProductAllotApplyDTO) > 0;
        if (!bRet) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("新增商品库存失败");
            return result;
        }
        result.setStatus(ResultCodeEM.OK);
        result.setMessage("新增商品库存成功");
        return result;
    }

    @Override
    public JsonResult updateProductInventory(@RequestBody UpdateProductAllotApplyDTO updateProductAllotApplyDTO) {
        JsonResult result = new JsonResult();
        if (updateProductAllotApplyDTO.getTypeName().isEmpty()
                || updateProductAllotApplyDTO.getTypeId() == null
                || updateProductAllotApplyDTO.getTypeId() <= 0
                || updateProductAllotApplyDTO.getDisposer().isEmpty()
                || updateProductAllotApplyDTO.getApplyStatus() == null
                || updateProductAllotApplyDTO.getApplyStatus() <= 0
                || updateProductAllotApplyDTO.getAllotNumber() == null
                || updateProductAllotApplyDTO.getAllotNumber() <= 0
                || updateProductAllotApplyDTO.getProductId() == null
                || updateProductAllotApplyDTO.getProductId() <= 0) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("参数不可为空");
            return result;
        }
        boolean bRet;
        bRet = productInventoryDao.updateProductInventory(updateProductAllotApplyDTO) > 0;
        if (!bRet) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("更新商品库存失败");
            return result;
        }
        result.setStatus(ResultCodeEM.OK);
        result.setMessage("更新商品库存成功");
        return result;
    }
    /**
     * 删除商品库存信息
     *
     * @param id 商品库存id
     * @return 操作结果
     * @author 季鑫利
     */
    @Override
    public JsonResult removeProductInventory(Integer id) {
        JsonResult result = new JsonResult();
        //判断参数合理性
        if (id == null || id <= 0) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("库存id异常");
            return result;
        }
        //定义布尔值
        boolean bRet;
        bRet = productInventoryDao.removeProductInventory(id) > 0;
        if (!bRet) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("逻辑删除商品库存失败");
            return result;
        }
        result.setStatus(ResultCodeEM.OK);
        result.setMessage("逻辑删除商品库存成功");
        return result;
    }

    /**
     * 查询所有商品库存
     *
     * @param pageDTO 分页参数
     * @return 所有商品库存
     * @author 季鑫利
     */
    @Override
    public JsonResult<PageVO<ProductInventory>> queryProductInventory(@RequestBody PageDTO pageDTO) {
        JsonResult result = new JsonResult();
        if (pageDTO.getPageNum() == null
                || pageDTO.getPageNum() <= 0
                || pageDTO.getTake() == null
                || pageDTO.getTake() <= 0) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("参数有误");
            return result;
        }
        PageVO<ProductInventory> pageVO = new PageVO<>();
        //库存出现条数
        int count = productInventoryDao.count();
        List<ProductInventory> productInventoryList = count > 0 ? productInventoryDao.queryProductInventory(pageDTO) : new ArrayList<>();
        pageVO.setCount(count);
        pageVO.setList(productInventoryList);
        result.setStatus(ResultCodeEM.OK);
        result.setData(productInventoryList);
        return result;
    }

    /**
     * 根据商品id查询库存
     *
     * @param id 商品id
     * @return 商品库存信息
     * @author 季鑫利
     */
    @Override
    public JsonResult<ProductInventory> queryProductInventoryById(Long id) {
        //检验库存id
        JsonResult result = new JsonResult();
        //判断参数合理性
        if (id == null || id <= 0) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("id异常");
            return result;
        }
        ProductInventory productInventory = productInventoryDao.queryProductInventoryById(id);
        //商品不存在
        if (productInventory == null) {
            result.setStatus(ResultCodeEM.NOCONTENT);
            result.setMessage("商品暂无库存");
            return result;
        }
        //操作成功
        result.setStatus(ResultCodeEM.OK);
        result.setData(productInventory);
        return result;
    }
}
